//
//  UIButton+LLAdd.m
//  JunKeDai
//
//  Created by DD on 2017/12/29.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "UIButton+LLAdd.h"

@implementation UIButton (LLAdd)

// 适配XIB中的字体
- (void)setFixWidthScreenFont:(float)fixWidthScreenFont
{
    if (fixWidthScreenFont > 0) {
        self.titleLabel.font = [UIFont systemFontOfSize:fixWidthScreenFont];
    } else {
        self.titleLabel.font = self.titleLabel.font;
    }
}

- (float)fixWidthScreenFont
{
    return self.fixWidthScreenFont;
}

+ (instancetype)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font backgroundColor:(UIColor *)backgroundColor target:(id)target action:(SEL)action
{
    return [self createButtonWithTitle:title normalTitleColor:titleColor selectedTitleColor:titleColor titleFont:font backgroundColor:backgroundColor cornerRadius:0 target:target action:action];
}




+ (instancetype)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font backgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)cornerRadius target:(id)target action:(SEL)action
{
    return [self createButtonWithTitle:title normalTitleColor:titleColor selectedTitleColor:titleColor titleFont:font backgroundColor:backgroundColor cornerRadius:cornerRadius target:target action:action];
}



+ (instancetype)createButtonWithTitle:(NSString *)title normalTitleColor:(UIColor *)normalTitleColor selectedTitleColor:(UIColor *)selectedTitleColor titleFont:(UIFont *)font backgroundColor:(UIColor *)backgroundColor target:(id)target action:(SEL)action
{
    return [self createButtonWithTitle:title normalTitleColor:normalTitleColor selectedTitleColor:selectedTitleColor titleFont:font backgroundColor:backgroundColor cornerRadius:0 target:target action:action];
}


+ (instancetype)createButtonWithTitle:(NSString *)title normalTitleColor:(UIColor *)normalTitleColor selectedTitleColor:(UIColor *)selectedTitleColor titleFont:(UIFont *)font backgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)cornerRadius target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:normalTitleColor forState:UIControlStateNormal];
    [btn setTitleColor:selectedTitleColor forState:UIControlStateSelected];
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    btn.titleLabel.font = font;
    btn.backgroundColor = backgroundColor;
    btn.layer.cornerRadius = cornerRadius;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}



+ (instancetype)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font normalBackgroundImage:(UIImage *)normalBackgroundImage disabledBackgroundImage:(UIImage *)disabledBackgroundImage target:(id)target action:(SEL)action
{
    return [self createButtonWithTitle:title titleColor:titleColor titleFont:font normalBackgroundImage:normalBackgroundImage disabledBackgroundImage:disabledBackgroundImage cornerRadius:0 target:target action:action];
}


+ (instancetype)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font normalBackgroundImage:(UIImage *)normalBackgroundImage disabledBackgroundImage:(UIImage *)disabledBackgroundImage cornerRadius:(CGFloat)cornerRadius target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    btn.titleLabel.font = font;
    [btn setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
    [btn setBackgroundImage:disabledBackgroundImage forState:UIControlStateDisabled];
    btn.layer.cornerRadius = cornerRadius;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font normalBackgroundImage:(UIImage *)normalBackgroundImage selectedBackgroundImage:(UIImage *)selectedBackgroundImage cornerRadius:(CGFloat)cornerRadius target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    btn.titleLabel.font = font;
    [btn setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
    [btn setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected];
    btn.layer.cornerRadius = cornerRadius;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)createButtonWithNormalTitle:(NSString *)normalTitle selectedTitle:(NSString *)selectedTitle titleColor:(UIColor *)titleColor titleFont:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:normalTitle forState:UIControlStateNormal];
    [btn setTitle:selectedTitle forState:UIControlStateSelected];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateSelected];
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    btn.titleLabel.font = font;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}



+ (instancetype)createButtonWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
    return [self createButtonWithNormalImage:image selectedImage:image cornerRadius:0 target:target action:action];
}



+ (instancetype)createButtonWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius target:(id)target action:(SEL)action
{
    return [self createButtonWithNormalImage:image selectedImage:image cornerRadius:cornerRadius target:target action:action];
}



+ (instancetype)createButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action
{
    return [self createButtonWithNormalImage:normalImage selectedImage:selectedImage cornerRadius:0 target:target action:action];
}


+ (instancetype)createButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage cornerRadius:(CGFloat)cornerRadius target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    btn.layer.cornerRadius = cornerRadius;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}



+ (instancetype)createButtonWithNormalImage:(UIImage *)normalImage disabledImage:(UIImage *)disabledImage target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:disabledImage forState:UIControlStateDisabled];
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)createButtonWithFrame:(CGRect)frame image:(UIImage *)image title:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font space:(CGFloat)space backgroundColor:(UIColor *)backgroundColor target:(id)target action:(SEL)action
{
    titleColor = titleColor == nil ? [UIColor blackColor] : titleColor;
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setImage:image forState:UIControlStateNormal];
    btn.imageView.contentMode = UIViewContentModeCenter;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.backgroundColor = backgroundColor;
    btn.titleLabel.font = font;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    CGFloat s = space * 0.5;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width - s , 0, btn.imageView.frame.size.width + s);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width + s, 0, -btn.titleLabel.frame.size.width - s);
    
    return btn;
}

+ (instancetype)createButtonWithFrame:(CGRect)frame image:(UIImage *)image title:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font backgroundColor:(UIColor *)backgroundColor target:(id)target action:(SEL)action
{
    titleColor = titleColor == nil ? [UIColor blackColor] : titleColor;
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
//    btn.imageView.contentMode = UIViewContentModeCenter;
    btn.backgroundColor = backgroundColor;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    CGFloat offset = 10.0f;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, -btn.imageView.frame.size.height-offset/2, 0);
    btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -btn.titleLabel.intrinsicContentSize.width);
    
    return btn;
}


+ (instancetype)createButtonImageBottomWithFrame:(CGRect)frame image:(UIImage *)image title:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font backgroundColor:(UIColor *)backgroundColor target:(id)target action:(SEL)action
{
    titleColor = titleColor == nil ? [UIColor blackColor] : titleColor;
    font = font == nil ? [UIFont systemFontOfSize:14] : font;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    btn.backgroundColor = backgroundColor;
    if (target != nil && action != nil) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    CGFloat offset = 6.0f;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, btn.imageView.frame.size.height+offset/2, 0);
    // 由于iOS8中titleLabel的size为0，用上面这样设置有问题，修改一下即可
    btn.imageEdgeInsets = UIEdgeInsetsMake(btn.titleLabel.intrinsicContentSize.height+offset/2, 0, 0, -btn.titleLabel.intrinsicContentSize.width);
    
    return btn;
    
}





+ (instancetype)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFontSize:(CGFloat)titleFontSize borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius contentEdgeInsets:(UIEdgeInsets)contentEdgeInsets target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:titleFontSize];
    btn.layer.cornerRadius = cornerRadius;
    btn.layer.borderColor = borderColor.CGColor;
    btn.layer.borderWidth = 0.5;
    btn.contentEdgeInsets = contentEdgeInsets;
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}




@end
